package com.uex.dev.manage.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.dao.ProjectOrderDao;
import com.uex.dev.manage.admin.intercepter.OpenidContext;
import com.uex.dev.manage.admin.intercepter.UserContext;
import com.uex.dev.manage.admin.po.DevicePO;
import com.uex.dev.manage.admin.po.ProjectOrderPO;
import com.uex.dev.manage.admin.po.ProjectPO;
import com.uex.dev.manage.admin.po.UserPO;
import com.uex.dev.manage.admin.service.IDeviceService;
import com.uex.dev.manage.admin.service.IProjectOrderService;
import com.uex.dev.manage.admin.service.IProjectService;
import com.uex.dev.manage.admin.utils.LogUtil;
import com.uex.dev.manage.admin.vo.wx.OpenidInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2024-02-22
 */
@Service
public class ProjectOrderServiceImpl extends ServiceImpl<ProjectOrderDao, ProjectOrderPO> implements IProjectOrderService {

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IProjectService projectService;

    @Override
    public boolean orderProject(ProjectOrderPO projectOrderReq) {
        Preconditions.checkArgument(projectOrderReq.getEndTime().isAfter(projectOrderReq.getStartTime()), "结束时间需在开始时间之后");
        ProjectPO project = projectService.getById(projectOrderReq.getPrjid());
        Preconditions.checkArgument(Objects.nonNull(project), "项目Id不存在");
        List<Integer> deviceIds = projectOrderReq.getDeviceIds();
        // 判断设备是否存在
        ArrayList<Integer> errIds = new ArrayList<>();
        LambdaQueryWrapper<DevicePO> wrapper = new LambdaQueryWrapper<DevicePO>()
                .eq(DevicePO::getDevicePrjid, projectOrderReq.getPrjid());
        List<DevicePO> list = deviceService.list(wrapper);
        ArrayList<Integer> matchList = new ArrayList<>();
        list.forEach(x -> matchList.add(x.getId()));
        deviceIds.forEach(x -> {
            if (!matchList.contains(x)) {
                errIds.add(x);
            }
        });
        String errMsg = errIds.toString();
        Preconditions.checkArgument(errIds.isEmpty(), String.format("项目 %s 不存在 设备Id %s", projectOrderReq.getPrjid().toString(), errMsg));
        // 更新设备维保时间
        List<DevicePO> deviceList = deviceService.listByIds(deviceIds);
        deviceList.forEach(x -> x.setDeviceMaintainTime(projectOrderReq.getEndTime()));
        deviceService.updateBatchById(deviceList);
        LogUtil.log(0, "设备购买维保");
        // 判断当前购买记录是否存在
        LambdaQueryWrapper<ProjectOrderPO> ByOrder = new LambdaQueryWrapper<ProjectOrderPO>()
                .eq(ProjectOrderPO::getPrjid, projectOrderReq.getPrjid());
        ProjectOrderPO order = this.getOne(ByOrder);
        // 记录存在
        if (order != null) {
            projectOrderReq.setId(order.getId());
            List<Integer> device = order.getDeviceIds();
            for (Integer id : device) {
                if (!deviceIds.contains(id)) {
                    deviceIds.add(id);
                }
            }
            return this.updateById(projectOrderReq);
        }
        // 获取小程序用户
        OpenidInfo openidInfo = OpenidContext.getOpenidLocal();
        UserPO webUser = UserContext.getUserInfoLocal();
        projectOrderReq.setCreateUser(webUser != null ? webUser.getId() : openidInfo.getUserId());
        projectOrderReq.setCreateTime(LocalDateTime.now());
        return this.save(projectOrderReq);
    }
}
